package com.gin.no_gay_alliance.adpter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import androidx.annotation.NonNull;

import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.database.entity.NgaAccount;
import com.gin.no_gay_alliance.databinding.ItemNgaAccountBinding;
import com.gin.no_gay_alliance.utils.AlertDialogUtils;
import com.gin.no_gay_alliance.utils.HandlerMessage;
import com.gin.no_gay_alliance.utils.ViewUtils;

import java.util.List;
import java.util.Locale;

/**
 * nga账号适配器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/4 11:53
 **/
public class NgaAccountAdapter extends BaseBindingRecyclerViewAdapter<ItemNgaAccountBinding, NgaAccount> implements View.OnLongClickListener, View.OnClickListener {
    public NgaAccountAdapter(Context context) {
        super(context);
    }

    public NgaAccountAdapter(@NonNull Context context, @NonNull List<NgaAccount> data) {
        super(context, data);
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param b        绑定对象
     * @param position 位置
     * @param item
     */
    @Override
    protected void onBindViewHolder(ViewHolder<ItemNgaAccountBinding> holder, @NonNull ItemNgaAccountBinding b, int position, NgaAccount item) {
        b.tvUsername.setText(item.username);
        b.tvUserId.setText(String.valueOf(item.userId));

        long currentUserId = MyApplication.app.accountManager.getCurrentUserId();
        ViewUtils.setGone(item.userId != currentUserId, b.iv);

        b.layoutParent.setTag(new DataItem(item, position));
        b.layoutParent.setOnClickListener(this);
        b.layoutParent.setOnLongClickListener(this);
    }

    /**
     * @return 是否应用斑马纹配色
     */
    @Override
    protected boolean useZebraStripe() {
        return false;
    }

    @Override
    public boolean onLongClick(View v) {
        DataItem dataItem = (DataItem) v.getTag();

        String message = String.format(Locale.CHINA, "确认移除账号 %s?", dataItem.account);
        AlertDialogUtils.showConfirm(context, "移除账号", message, (dialog, which) -> {
            if (which == DialogInterface.BUTTON_POSITIVE) {
                MyApplication.app.accountManager.removeAccount(dataItem.account.userId);
                data.remove(dataItem.position);
                notifyItemRemoved(dataItem.position);
            }
        });
        return true;
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onClick(View v) {
        DataItem dataItem = (DataItem) v.getTag();

        handler.sendMessage(handler.obtainMessage(HandlerMessage.UPDATE_CURRENT_USER_ID, dataItem.account.userId));
        MyApplication.app.accountManager.setCurrentUser(dataItem.account.userId);
        handler.postDelayed(this::notifyDataSetChanged, 200);
    }

    public static class DataItem {
        public NgaAccount account;
        public int position;

        public DataItem(NgaAccount account, int position) {
            this.account = account;
            this.position = position;
        }
    }
}